package br.com.simora.crawler;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import javax.inject.Named;

import org.apache.log4j.Logger;

import br.com.simora.musica.Artista;
import br.com.simora.musica.Musica;
import br.com.simora.radio.RadioEnum;

@Named("jbfm")
public class JBFMCrawler extends AbstractCrawler {
	private static Logger logger = Logger.getLogger(JBFMCrawler.class);
	private static String URL = "http://jbfm.ig.com.br/tools/marquee.html?174784153";
	
	@Override
	public void recuperaInfo(){
		recuperador.setUrl(URL, "html");
		pagina = recuperador.getPagina();
		String[] itensConteudo = pagina.getConteudo().split("-");
		String artistaNome = "";
		String musicaNome = "";
		if(itensConteudo.length > 0){
			if(!"Aguarde".equals(itensConteudo[0].trim())){
				try {
					artistaNome = new String(itensConteudo[0].trim().getBytes(Charset.forName(pagina.getContentType())), pagina.getContentType())
					.replace("&#039;", "'");
					artista = new Artista();
					artista.setNome(artistaNome);
				} catch (UnsupportedEncodingException e) {
					logger.debug(e.getMessage());
					e.printStackTrace();
				}
				if (null != itensConteudo[1]){
					if(itensConteudo[1].contains("|")){
						String[] dadosMusica = itensConteudo[1].split("\\|");
						if(dadosMusica.length > 0){
							try {
								musicaNome = new String(dadosMusica[0].trim().getBytes(Charset.forName(pagina.getContentType())), pagina.getContentType())
								.replace("&#039;", "'");
								musica = new Musica();
								musica.setNome(musicaNome);
							} catch (UnsupportedEncodingException e) {
								logger.debug(e.getMessage());
								e.printStackTrace();
							}
							
						}
					}
				}
			}
		}
		logger.debug(artistaNome + " - " + musicaNome);
	}

	@Override
	public void setRadioNome() {
		this.radioNome = RadioEnum.jbfm.getNome();
	}

}